package com.jhhe.homework.UserStat

case class UserInfo(userName:String,location:String,startTime:Int,duration:Int)
/*现有如下数据需要处理：
字段：用户ID，位置ID，开始时间，停留时长（分钟）
4行样例数据：
     UserA,LocationA,8,60
     UserA,LocationA,9,60
     UserB,LocationB,10,60
     UserB,LocationB,11,80
*/
object UserStat {
  def main(args: Array[String]): Unit = {
    val userInfoList:List[UserInfo] = List(
      UserInfo("UserA", "LocationA", 8, 60),
      UserInfo("UserA", "LocationA", 9, 60),
      UserInfo("UserA", "LocationB", 10, 60),
      UserInfo("UserA", "LocationB", 11, 80)
    )


    val userMap = userInfoList.groupBy(t => t.userName+","+t.location)
    val orderByUserMap = userMap.mapValues(t =>t.sortBy(x=>x.startTime))

    var firstTime = 0

    val totalMap = orderByUserMap.mapValues(t =>{
      firstTime = t.head.startTime
      var sum = t.map(x=> x.duration).sum
      sum
    })
    totalMap.foreach{
      case (datas,sumTime)=>println(s"$datas,$firstTime,$sumTime")
    }
  }
}
